google.load('visualization', '1', {'packages':['corechart']});

var data = null;

function changeData() {
  var queryText = encodeURIComponent("SELECT Country, Health, Economy, Final from 105221 order by Final DESC limit 10");
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
  query.send(getData);
}

function changeDataUserSelect() {
	var limit_str = document.getElementById('limit').value;
	if(!isNumber(limit_str)){
		alert("Invalid number of countries to display");
		document.getElementById('limit').value = '';
		return;
	}
	var queryText = encodeURIComponent("SELECT Country, Health, Economy, Final from 105221 order by Final DESC limit " + limit_str);
	var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
	query.send(getData);
}
function clearUserData(){
	removeChildrenFromNode(document.getElementById('usertable'));
	document.getElementById('fusion').style.display='';
	document.getElementById('user').style.display='none';
}

function getData(response) {
	clearUserData();

 numRows = response.getDataTable().getNumberOfRows();
  numCols = response.getDataTable().getNumberOfColumns();

  fusiontabledata = '<table><tr>';
  for(var i = 0; i < numCols; i++) {
    fusiontabledata += '<th>' + response.getDataTable().getColumnLabel(i) + '</th>';
  }
  fusiontabledata += '</tr>';

  for(i = 0; i < numRows; i++) {
    fusiontabledata += '<tr>';
    for(j = 0; j < numCols; j++) {
      fusiontabledata += '<td>' + response.getDataTable().getValue(i, j) + '</td>';
    }
    fusiontabledata += '</tr>';
  }
  fusiontabledata += '</table>'

  document.getElementById('ftdata').innerHTML = fusiontabledata;

  fusiontabledata = '<p>';
   var denominator_1 = response.getDataTable().getValue(0, numCols-1);


    for(i = 1; i < numRows; i++) {
    	fusiontabledata += response.getDataTable().getValue(i, 0) + ": ";
  		var denominator_2 = response.getDataTable().getValue(i, numCols-1);
  		var numerator_2 = parseFloat(denominator_2 / denominator_1 );
    	var	numerator = numerator_2 - 1;
      	for(var j = 1; j < numCols-1; j++) {
			var r_2 = response.getDataTable().getValue(0, j);
			var r_1 = response.getDataTable().getValue(i, j);
			var denom = parseFloat(r_1/r_2);
			denom = denom -= 1;

			var correlation = parseFloat(numerator/denom);
//			correlation = 1/correlation;

			fusiontabledata += correlation + ",";
      	}
      	fusiontabledata += "<br />";
  	}
document.getElementById('ftdata2').innerHTML = fusiontabledata;

  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(response.getDataTable(),
           {title:"Quality of Life By Country",
            width:600, height:400,
            vAxis: {title: "Data"},
            hAxis: {title: "Country"}}
      );
}

function removeChildrenFromNode(node)
{
	while (node.hasChildNodes())
	{
	  node.removeChild(node.firstChild);
	}
}
function switchToUser(){
	document.getElementById('fusion').style.display='none';
	document.getElementById('user').style.display='';
	data = new google.visualization.DataTable();
	var userDataGrid = new userDataGrid();
	userDataGrid.init();
}
// Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {


        // Instantiate and draw our chart, passing in some options.
        chart = new google.visualization.LieChart(document.getElementById('chart_div'));
        google.visualization.events.addListener(chart, 'select', selectHandler);
        chart.draw(data, options);
      }

      function selectHandler() {
        var selectedItem = chart.getSelection()[0];
        var value = data.getValue(selectedItem.row, 0);
        alert('The user selected ' + value);
      }
